<?php
	//get e-member id
	//get member id
	
	function dk_eshop_get_e_member_id(){
		global $auth;
		return $member_id = $auth->getUserInfo('member_id');
	}
	
	//menu functions
	function wp_dk_estore_product_management_menu(){
		require_once(DK_PLUGIN_FUNCTION_DIR . 'product_management_menu.php');
			if(!in_array('Catalogue Addon',dk_eshop_get_e_member_arr())){
				echo 'Sorry';
			}else{
				wp_dk_estore_product_management_menu_tpl();
			}
	}
	
	function wp_dk_estore_add_product_menu(){
		require_once(DK_PLUGIN_FUNCTION_DIR . 'add_product_menu.php');
		if(!in_array('Catalogue Addon',dk_eshop_get_e_member_arr())){
				echo 'Sorry';
		}else{
			wp_dk_estore_add_product_menu_tpl();
		}
	}
	
	function wp_dk_eStore_manage_categories_menu(){
		require_once(DK_PLUGIN_FUNCTION_DIR . 'manage_categories_menu.php');
		if(!in_array('Catalogue Addon',dk_eshop_get_e_member_arr())){
				echo 'Sorry';
		}else{
			wp_dk_eStore_manage_categories_menu_tpl();
		}
	}
	
	function wp_dk_estore_customer_management_menu(){
		require_once(DK_PLUGIN_FUNCTION_DIR . 'customers_menu.php');
		if(!in_array('Catalogue Addon',dk_eshop_get_e_member_arr())){
				echo 'Sorry';
		}else{
			wp_dk_estore_customer_management_menu_tpl();
		}
	}
	
	function wp_dk_estore_add_customer_menu(){
		require_once(DK_PLUGIN_FUNCTION_DIR . 'customers_menu.php');
		if(!in_array('Catalogue Addon',dk_eshop_get_e_member_arr())){
				echo 'Sorry';
		}else{
			wp_dk_estore_add_customer_menu_tpl();
		}
	}
	
	
//get e-member role return array
function dk_eshop_get_e_member_arr(){
	global $auth;
	global $emember_config;
	$all_levels = dbAccess::findAll(WP_EMEMBER_MEMBERSHIP_LEVEL_TABLE, ' id != 1 ', ' id DESC ');
	$member_id = $auth->getUserInfo('member_id'); 
	$editingrecord = dbAccess::find(WP_EMEMBER_MEMBERS_TABLE_NAME, ' member_id=' . $member_id);
	$user_role_arr = array();
	
	
	foreach($all_levels as $level){
		if($editingrecord->membership_level ===$level->id){
			$user_role_arr[] = $level->alias;
		}
	}
	
	$more_membership_arr = explode(",",$editingrecord->more_membership_levels);
	foreach($all_levels as $level){
		if(in_array($level->id ,$more_membership_arr)){	
			$user_role_arr[] = $level->alias;
		}		
	}
	return $user_role_arr;
}


//delete products belong to user
function dk_eshop_delete_product_belong_user($proid){
	//delete product belong to user
	$user_products = get_option('dk_eshop_user_products_' . dk_eshop_get_e_member_id());
			
	if(empty($user_products))
		$user_products = array();

	if(in_array($proid,$user_products)){
		foreach($user_products as $pkey => $pvalue){
			if($pvalue == $proid)
				unset($user_products[$pkey]);
			}
		}
	update_option( 'dk_eshop_user_products_' . dk_eshop_get_e_member_id() , $user_products);			
}
?>